package service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import commentControl.Comment;
import commentControl.CommentDao;
import commentControl.CommentListView;

import util.mysqlUtil;

public class GetComment {
	String tableName;

	public GetComment(String tableName) {
		this.tableName = tableName;
	}

	public CommentListView getCommentList(int number) throws ServiceException {
		Connection conn = null;
		try {
			conn = mysqlUtil.getConn();
			CommentDao commentdao = new CommentDao(tableName);
			int commentCount = commentdao.selectCount(conn, number);
			List<Comment> commentList = null;
			if (commentCount == 0) {
				commentList = Collections.emptyList();
			} else {
				commentList = commentdao.selectList(conn, number);
			}
			return new CommentListView(commentList, commentCount, number);
		} catch (SQLException e) {
			throw new ServiceException("댓글 목록 구하기 실패: " + e.getMessage(), e);
		} finally {
			mysqlUtil.close(conn);
		}
	}
}
